﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GN.Pay.Application.GatewayApp;
using GN.Pay.Utils;
using GN.Pay.TradeChannel;
using GN.Pay.TradeChannel.UnionPay;

namespace GN.Pay.GatewayWeb.Controllers.Notify
{
    /// <summary>
    /// 银联
    /// </summary>
    public class UnionPayController : APayNotifyController
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tradeNotifyAppService"></param>
        /// <param name="systemQueryAppService"></param>
        public UnionPayController(ITradeNotifyAppService tradeNotifyAppService)
            : base(tradeNotifyAppService)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        protected override string ChannelId
        {
            get
            {
                return "Union.WebPay";
            }
        }

        protected override NotifyMessage GetPayNotifyMessage(HttpRequestBase request)
        {
            var requestDictionary = PreParam.GetQueryDictionary(request);
            var no = UnionPayServiceBase.GetPayOrderNo(requestDictionary);
            return new NotifyMessage(no, requestDictionary);
        }

        protected override NotifyMessage GetRefundNotifyMessage(HttpRequestBase request)
        {
            var requestDictionary = PreParam.GetQueryDictionary(request);
            var no = UnionPayServiceBase.GetRefundNo(requestDictionary);
            return new NotifyMessage(no, requestDictionary);
        }

        public override void OrderPay()
        {
            DictionaryPayNotify();
        }

        private ActionResult OrderPayReturn(Func<ActionResult> errorActionResult, Func<string, ActionResult> actionResult)
        {
            var msg = GetPayNotifyMessage(Request);
            ITradeChannel channel = GetPayTradeChannel(msg);
            if (channel != null)
            {
                try
                {
                    channel.OrderPayNotify(this.tradeNotifyAppService, msg.Message);
                    UnionPayWebPayService payService = (UnionPayWebPayService)channel;
                    if (payService == null)
                    {
                        return this.ErrorView();
                    }
                    var entity = payService.OrderPayNotifyResponse((IDictionary<string, string>)msg.Message);
                    return ReturnWaitResult(entity.orderId);
                }
                catch (Exception err)
                {
                    Logger.Error("银联付款通知错误", err);
                    return this.ErrorView();
                }
            }
            else
            {
                Logger.Error("银联付款通知错误,无 " + ChannelId + " 实现");
                return this.ErrorView();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override ActionResult OrderPayReturn()
        {
            var msg = GetPayNotifyMessage(Request);
            ITradeChannel channel = GetPayTradeChannel(msg);
            if (channel != null)
            {
                try
                {
                    channel.OrderPayNotify(this.tradeNotifyAppService, msg.Message);
                    UnionPayWebPayService payService = (UnionPayWebPayService)channel;
                    if (payService == null)
                    {
                        return this.ErrorView();
                    }
                    var entity = payService.OrderPayNotifyResponse((IDictionary<string, string>)msg.Message);
                    return ReturnWaitResult(entity.orderId);
                }
                catch (Exception err)
                {
                    Logger.Error("银联付款通知错误", err);
                    return this.ErrorView();
                }
            }
            else
            {
                Logger.Error("银联付款通知错误,无 " + ChannelId + " 实现");
                return this.ErrorView();
            }
        }

        public override void OrderRefund()
        {
            DictionaryRefundNotify();
        }

        public override ActionResult OrderPayWapReturn()
        {
            throw new NotImplementedException();
        }
    }
}